/*扩充字符串函敄*/
if (typeof String.prototype.trim !== "function") {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, "");
	};
}
/*扩充数组函敄*/
Array.prototype.contains = function(element) {
	for ( var i = this.length - 1; i >= 0; i--) {
		if (this[i] == element) {
			return true;
		}
	}
	return false;
};
/* 日期格式化 */
// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt) { // author: meizz
	var o = {
		"M+" : this.getMonth() + 1, /* 月份 */
		"d+" : this.getDate(), /* 日 */
		"H+" : this.getHours(), /* 小时 */
		"m+" : this.getMinutes(), /* 分 */
		"s+" : this.getSeconds(), /* 秒 */
		"q+" : Math.floor((this.getMonth() + 3) / 3), /* 季度 */
		"S" : this.getMilliseconds()/* 毫秒 */
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
};
/**
 * 刷新页面
 */
function refresh(){
	location.href = location.href;
}
/* 实现将对象转换成字符串 */
var parseParam = function(param, key) {
	var paramStr = "";
	if (param instanceof String || param instanceof Number || param instanceof Boolean) {
		paramStr += "&" + key + "=" + encodeURIComponent(param);
	} else {
		$.each(param, function(i) {
			var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
			paramStr += '&' + parseParam(this, k);
		});
	}
	return paramStr.substr(1);
};
/* 获取随机数，介于m和n之间，m和n的默认 1和100 */
function getRand(m, n) {
	var x = m ? m : 1;
	var y = n ? n : 100;
	var rand = parseInt(Math.random() * (y - x) + x);
	return rand;
}
/*检测浏览器版本和型号*/
function checkNavigator(){
	var NG = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? NG.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) ? NG.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? NG.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) ? NG.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) ? NG.safari = s[1] : 0;
    if(!NG.chrome){
    	showMsg("", "请使用谷歌 <a href='res/download/CHSB_Chrome_non_defaultV3.exe' target='blank'>Chrome</a>(点此下载) 浏览器访问此系统！", true, "warning");
    }
}
var cookieName_username = "LOGIN_USER_NAME";
var cookieName_password = "LOGIN_PASSWORD";
/* 保存到cookie */
function setCookie(name, value, days) {
	var exp = new Date();
	exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
/* 从cookie取值 */
function getCookie(name) {
	var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (arr != null) {
		return unescape(arr[2]);
	}
	return null;
}
/* 删除cookie */
function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	document.cookie = name + "=;expires=" + exp.toGMTString();
}
/* 打开新页面 */
function openpage(url, random) {
	/* 添加随机参数 */
	var ctm = getRand(1, 10000);
	if (random != undefined && random == true) {
		if (url.indexOf("?") != -1) {
			url += "&ctm=" + ctm;
		} else {
			url += "?ctm=" + ctm;
		}
	}
	window.open(url, "main");
}
/*---------------------------------------------Map BEGIN----------------------------------------------------*/
/**
 * Map的构造函数
 */
function Map() {
	this.container = new Object();
}
/**
 * 添加一个键-值
 */
Map.prototype.put = function(key, value) {
	this.container[key] = value;
}
/**
 * 通过键获取一个值
 */
Map.prototype.get = function(key) {
	return this.container[key];
}
/**
 * 返回该Map对象的键集合
 */
Map.prototype.keySet = function() {
	var keyset = new Array();
	var count = 0;
	for ( var key in this.container) {
		/* 跳过object的extend函数 */
		if (key == 'extend') {
			continue;
		}
		keyset[count] = key;
		count++;
	}
	return keyset;
}
/**
 * 返回该Map对象的大小
 */
Map.prototype.size = function() {
	var count = 0;
	for ( var key in this.container) {
		/* 跳过object的extend函数 */
		if (key == 'extend') {
			continue;
		}
		count++;
	}
	return count;
}
/**
 * 删除Map对象
 */
Map.prototype.remove = function(key) {
	delete this.container[key];
}
/**
 * 返回该Map对象的字符串形式
 */
Map.prototype.toString = function() {
	var str = "";
	for ( var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
		str = str + keys[i] + "=" + this.container[keys[i]] + ";\n";
	}
	return str;
}
/*---------------------------------------------Map END----------------------------------------------------*/

/*---------------------------------------------回到顶部 END----------------------------------------------------*/
var scrolltotop = {
	setting : {
		startline : 1,
		scrollto : 0,
		scrollduration : 1000,
		fadeduration : [ 500, 100 ]
	},
	controlHTML : '<img src="img/gotop.gif" style="width:19px; height:63px" />',
	controlattrs : {
		offsetx : 100,
		offsety : 120
	},
	anchorkeyword : '#top',

	state : {
		isvisible : false,
		shouldvisible : false
	},

	scrollup : function() {
		if (!this.cssfixedsupport) {
			this.$control.css({
				opacity : 0
			});
		}
		var dest = isNaN(this.setting.scrollto) ? this.setting.scrollto : parseInt(this.setting.scrollto);
		if (typeof dest == "string" && jQuery('#' + dest).length == 1) {
			dest = jQuery('#' + dest).offset().top;
		} else {
			dest = 0;
		}
		this.$body.animate({
			scrollTop : dest
		}, this.setting.scrollduration);
	},

	keepfixed : function() {
		var $window = jQuery(window);
		var controlx = $window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx;
		var controly = $window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety;
		this.$control.css({
			left : controlx + 'px',
			top : controly + 'px'
		});
	},

	togglecontrol : function() {
		var scrolltop = jQuery(window).scrollTop();
		if (!this.cssfixedsupport) {
			this.keepfixed();
		}
		this.state.shouldvisible = (scrolltop >= this.setting.startline) ? true : false;
		if (this.state.shouldvisible && !this.state.isvisible) {
			this.$control.stop().animate({
				opacity : 1
			}, this.setting.fadeduration[0]);
			this.state.isvisible = true;
		} else if (this.state.shouldvisible == false && this.state.isvisible) {
			this.$control.stop().animate({
				opacity : 0
			}, this.setting.fadeduration[1]);
			this.state.isvisible = false;
		}
	},

	init : function() {
		jQuery(document).ready(function($) {
			var mainobj = scrolltotop;
			var iebrws = document.all;
			mainobj.cssfixedsupport = !iebrws || iebrws && document.compatMode == "CSS1Compat" && window.XMLHttpRequest;
			mainobj.$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
			mainobj.$control = $('<div id="topcontrol">' + mainobj.controlHTML + '</div>').css({
				position : mainobj.cssfixedsupport ? 'fixed' : 'absolute',
				bottom : mainobj.controlattrs.offsety,
				right : mainobj.controlattrs.offsetx,
				opacity : 0,
				cursor : 'pointer'
			}).attr({
				title : '返回顶部'
			}).click(function() {
				mainobj.scrollup();
				return false;
			}).appendTo('body');
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text() != '') {
				mainobj.$control.css({
					width : mainobj.$control.width()
				});
			}
			mainobj.togglecontrol();
			$('a[href="' + mainobj.anchorkeyword + '"]').click(function() {
				mainobj.scrollup();
				return false;
			});
			$(window).bind('scroll resize', function(e) {
				mainobj.togglecontrol();
			});
		});
	}
};
//scrolltotop.init();
/*---------------------------------------------回到顶部 END----------------------------------------------------*/
